﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using EHTWebsite.Code;
using System.Web.Profile;
using EHTWebsiteProject.Code.Entity;
using System.Web.Security;

namespace EHTWebsiteProject
{
    public partial class ChangeServiceLevel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    ProfileBase theProfile = ProfileBase.Create(HttpContext.Current.User.Identity.Name);
                    txtServiceLevel.Text = ServiceLevelsProvider.GetServiceLevelByID(Int32.Parse(theProfile.GetPropertyValue("ServiceLevelID").ToString())).Description;
                    
                    DataSet dsLevelServices = ServiceLevelsProvider.GetServiceLevels();
                    this.ddlServiceLevelChange.DataValueField = "SeviceLevelID";
                    this.ddlServiceLevelChange.DataTextField = "Description";
                    ddlServiceLevelChange.DataSource = dsLevelServices;
                    ddlServiceLevelChange.DataBind();
                    this.ddlServiceLevelChange.Items.Insert(0, new ListItem("-- Level Service --", "-1"));

                    MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                    if (theUser == null)
                        Response.Redirect("Hjem.html");
                    else
                    {
                        DataSet dsSVEffect = ServiceLevelsProvider.getServicesLevelEffectByCustomerID(theUser.ProviderUserKey.ToString());
                        if (dsSVEffect.Tables[0].Rows.Count != 0)
                        {
                            this.ddlServiceLevelChange.SelectedValue = dsSVEffect.Tables[0].Rows[0]["SeviceLevelCostID"].ToString();
                        }
                        else
                        {
                            this.ddlServiceLevelChange.SelectedValue = "-1";
                        }
                    }
                    if (Request.QueryString["MessageChange"] != null)
                    {
                        ltrMessageSuccess.Text = Request.QueryString["MessageChange"].ToString();
                    }
                }
                catch
                {
                }
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            try
            {
                ServicesLevelEffect theServicesLevelEffect = new ServicesLevelEffect();
                    theServicesLevelEffect.SeviceLevelCostID = Int32.Parse(this.ddlServiceLevelChange.SelectedValue);
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                if (theUser == null)
                    Response.Redirect("Hjem.html");
                else
                {
                    theServicesLevelEffect.CustomerID = theUser.ProviderUserKey.ToString();
                    theServicesLevelEffect.ServiceCostEffect = ServiceLevelsProvider.GetServiceLevelByID(Int32.Parse(this.ddlServiceLevelChange.SelectedValue)).ServiceCost;
                    theServicesLevelEffect.Description = "";
                    DataSet dsSVEffect = ServiceLevelsProvider.getServicesLevelEffectByCustomerID(theUser.ProviderUserKey.ToString());
                    if (dsSVEffect.Tables[0].Rows.Count == 0)
                    {
                        ServiceLevelsProvider.IUServiceLevelsEffect(theServicesLevelEffect, true);
                    }
                    else
                    {
                        ServiceLevelsProvider.IUServiceLevelsEffect(theServicesLevelEffect, false);
                    }
                }
                ltrMessageSuccess.Text = "Endre suksess"; 
                Response.Redirect("EndreServiceLevel-"+ltrMessageSuccess.Text+".html");
            }
            catch
            {
                ltrMessageFail.Text = "Feil under endring";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Kundeprofil.html");
        }

        protected void ddlServiceLevelChange_TextChanged(object sender, EventArgs e)
        {
            ltrMessageFail.Text = "";
            ltrMessageSuccess.Text = "";
        }
    }
}